package com.ruoyi.xh.service.impl;

import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.ObjectId;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.PageUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.xh.mapper.XhCityMapper;
import com.ruoyi.xh.domain.XhCity;
import com.ruoyi.xh.service.IXhCityService;

/**
 * 城市Service业务层处理
 *
 * @author songjw
 * @date 2024-12-13
 */
@Service
public class XhCityServiceImpl extends ServiceImpl<XhCityMapper, XhCity> implements IXhCityService {

    /**
     * 查询城市
     *
     * @param id 城市主键
     * @return 城市
     */
    @Override
    public XhCity selectXhCityById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询城市列表
     *
     * @param xhCity 城市
     * @return 城市
     */
    @Override
    public Page<XhCity> selectXhCityList(XhCity xhCity) {
        return this.page(PageUtils.buildPage(), Wrappers.lambdaQuery(XhCity.class).
                like(StrUtil.isNotBlank(xhCity.getName()), XhCity::getName, xhCity.getName()).
                eq(ObjectUtil.isNotEmpty(xhCity.getPid()), XhCity::getPid, xhCity.getPid()));
    }

    /**
     * 新增城市
     *
     * @param xhCity 城市
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertXhCity(XhCity xhCity) {
        xhCity.setCreareTime(DateUtil.currentSeconds());
        //所属国家
        xhCity.setSid(ObjectUtil.defaultIfNull(xhCity.getSid(), 1L));
        xhCity.setShopId(ObjectUtil.defaultIfNull(xhCity.getShopId(), 0L));
        xhCity.setPid(1L);
        xhCity.setLevel(1L);
        xhCity.setHierarchy(2L);
        Opt.ofNullable(xhCity.getPid()).ifPresent(c -> {
            XhCity parentXhCity = this.getById(c);
            xhCity.setLevel(ObjectUtil.defaultIfNull(parentXhCity, XhCity::getLevel, 0L) + 1L);
            xhCity.setHierarchy(ObjectUtil.defaultIfNull(parentXhCity, XhCity::getHierarchy, 1L) + 1L);
            xhCity.setDistrictSqe(ObjectUtil.defaultIfNull(parentXhCity, XhCity::getDistrictSqe, ".1."));
        });
        this.save(xhCity);
        xhCity.setDistrictSqe(xhCity.getDistrictSqe() + xhCity.getId() + ".");
        this.updateById(xhCity);
    }

    /**
     * 修改城市
     *
     * @param xhCity 城市
     */
    @Override
    public void updateXhCity(XhCity xhCity) {
        this.updateById(xhCity);
    }

    /**
     * 批量删除城市
     *
     * @param ids 需要删除的城市主键
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteXhCityByIds(Long[] ids) {
        this.removeByIds(Stream.of(ids).collect(Collectors.toSet()));
    }
}
